/**
 * 类名：DeleteAction
 * 创建日期： 2014-11-19 下午8:18:13
 * 
 */
package com.erms.action;

import javax.annotation.Resource;

import com.erms.bean.Courseware;
import com.erms.bean.Task;
import com.erms.bean.Message;
import com.erms.dao.TeachDAO;
import com.erms.domain.CoursewareService;
import com.erms.domain.HomeworkService;
import com.erms.domain.MessageService;
import com.erms.domain.TaskService;
import com.erms.util.StringUtil;

/**
 * @author hmb 备注：
 * 
 */
public class DeleteAction extends BaseAction {

	private static final long serialVersionUID = -5690294089623881776L;

	@Resource
	private CoursewareService coursewareService;
	@Resource
	private MessageService messageService;
	@Resource
	private HomeworkService homeworkService;
	@Resource
	private TaskService taskService;
	@Resource
	private TeachDAO teachDAO;

	private Integer id;

	public String deleteCourseware() {

		String userName = getUserNameFromSession();
		if (StringUtil.isNull(userName)) {
			return LOGIN;
		}
		// TODO 这里要做权限验证和与客户端的交互
		Courseware courseware = coursewareService.find(id);

		if (courseware != null) {
			String uploadUserID = courseware.getUser().getId();
			logger.debug("当前用户：" + userName + ",待删除的课件上传者id：" + uploadUserID);
			if (userName.equals(uploadUserID)) {

				if (coursewareService.delete(id)) {

					logger.debug("成功删除");
				}
			}

		}

		return SUCCESS;
	}

	public String deleteMessage() {

		String userName = getUserNameFromSession();
		if (StringUtil.isNull(userName)) {
			return LOGIN;
		}

		Message message = messageService.find(id);
		
		if(message != null) {
			String teacherID =  message.getTeach().getTeacherId();
			if(userName.equals(teacherID)) {
				if(messageService.delete(id)) {}
			}
		}
		
		return SUCCESS;
	}

	public String deleteTask() {
		String type = getTypeFormSession();
		if (!TEACHER.equals(type)) {
			//TODO 可能需要其他处理方案
			return SUCCESS;
		}
		Task task = taskService.find(id);
		if (task != null) {
			logger.debug("即将删除作业：id="+task.getId()+";title="+task.getTitle());
			if (taskService.delete(id)) {
				logger.debug("成功删除");
			}
		}
		return SUCCESS;
	}

	public String deleteHomework() {

		return SUCCESS;
	}

	public Integer getId() {

		return id;
	}

	public void setId(Integer id) {

		this.id = id;
	}
}
